﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Popup.Master" AutoEventWireup="true"
    CodeBehind="TS_ApproveOT.aspx.cs" Inherits="vHCS.K2App.MdlK2.TS_ApproveOT" %>

<%@ Register Src="../MdlK2/Controls/HR_ctrEmpHeader_Info.ascx" TagPrefix="uc" TagName="EmpHeader" %>
<%@ Register Assembly="Core.Controls" Namespace="Core.Controls.DataReader" TagPrefix="core" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register Assembly="Core.Controls" Namespace="Core.Controls.Message" TagPrefix="msg" %>
<%@ Register Assembly="Core.Controls" Namespace="Core.Controls.Security" TagPrefix="sec" %>
<%@ Register Assembly="Core.Controls" Namespace="Core.Controls.Validate" TagPrefix="core" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainToolbar" runat="server">
    <div class="divButton">
        <asp:Button ID="btnSaveTop" runat="server" Text="Lưu" CssClass="btnSave" ValidationGroup="validateSave"
            OnClick="btnSave_Click" />
    </div>
    <msg:MessageSetting ID="msgList" runat="server">
        <Items>
            <msg:Message Code="10081" />
            <msg:Message Code="10010" />
        </Items>
    </msg:MessageSetting>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div class="searchWrapper">
        <uc:EmpHeader ID="ucSerach" runat="server" />
    </div>
    <div style="display: none">
        <asp:TextBox ID="txtEmpID" runat="server" Text=""></asp:TextBox>
        <asp:TextBox ID="txtMonthYear" runat="server" Text=""></asp:TextBox>
        <asp:TextBox ID="txtOverTimeID" runat="server" Text=""></asp:TextBox>
    </div>
    <div class="divClear">
    </div>
    <%-- <div class="divButton">
        <asp:Button ID="btnSearch" runat="server" Text="Tìm kiếm"  CssClass="btnFind"/>
        <asp:Button ID="btnRefesh" runat="server" Text="Làm lại" CssClass="btnRefresh" />
    </div>--%>
    <div style="display: none" id="divValid">
        <asp:Label ID="lblValid" runat="server" CssClass="validation" Text="Vui lòng nhập số."></asp:Label>
    </div>
    <div style="display: none" id="divOTDayApprove">
        <asp:Label ID="Label3" runat="server" CssClass="validation" Text="Vui lòng nhập OT ngày duyệt(số)."></asp:Label>
    </div>
    <div style="display: none" id="divOTNightApprove">
        <asp:Label ID="Label4" runat="server" CssClass="validation" Text="Vui lòng nhập OT đêm duyệt(số)."></asp:Label>
    </div>
    <div style="display: none" id="divCheckOtDay">
        <asp:Label ID="Label1" runat="server" CssClass="validation" Text="Vui lòng nhập OT ngày duyệt >=0 và <= OT ngày thực tế"></asp:Label>
    </div>
    <div style="display: none" id="divCheckOTNight">
        <asp:Label ID="Label2" runat="server" CssClass="validation" Text="Vui lòng nhập OT đêm duyệt >=0 và <= OT đêm thực tế"></asp:Label>
    </div>
    <div style="display: none">
        <asp:CustomValidator ID="valGrid" runat="server" ValidationGroup="validateSave" Display="Dynamic"
            CssClass="validation" ClientValidationFunction="dtgList_Validate" OnItemDataBound="dtg_ItemDataBound"
            ErrorMessage="">
        </asp:CustomValidator>
    </div>
    <div class="div4Column">
        <div class="label">
            <asp:Label ID="Label82" runat="server" Text="Trạng thái"></asp:Label>
        </div>
        <div class="control">
            <telerik:RadComboBox ID="cboStatus" runat="server" Width="50%" DataSourceID="dtsStatusApprove"
                DataTextField="Name" DataValueField="Value">
            </telerik:RadComboBox>
            <core:CoreDataSource ID="dtsStatusApprove" runat="server" SelectMethod="GetActions"
                TypeName="K2CoreLib.K2Utils" EnableCaching="False"
                MaximumRowsParameterName="">
                <SelectParameters>
                    <asp:QueryStringParameter Name="sn" QueryStringField="SN" DefaultValue="" />
                    <asp:QueryStringParameter Name="sharedUser" QueryStringField="SharedUser" DefaultValue="" />
                </SelectParameters>
            </core:CoreDataSource>
        </div>
        <div class="divClear">
        </div>
    </div>
    <div class="div6Column">
        <div class="divGrid" style="overflow-x: scroll; overflow-y: hidden;">
            <telerik:RadGrid ID="dtg" runat="server" DataSourceID="VacationList" AutoGenerateColumns="false"
                Width="140%" Height="400" AllowPaging="true" AllowMultiRowSelection="true" OnItemDataBound="dtg_ItemDataBound">
                <MasterTableView>
                    <Columns>
                        <telerik:GridTemplateColumn HeaderStyle-Width="5%" HeaderText="STT" AllowFiltering="false"
                            ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <%# Container.ItemIndex + 1 + dtg.PageSize * dtg.CurrentPageIndex%>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridBoundColumn DataField="OverTimeID" HeaderText="" Visible="false" />
                        <telerik:GridBoundColumn DataField="EmpID" HeaderText="" Visible="false" />
                        <telerik:GridBoundColumn DataField="IsCompensation" HeaderText="" Visible="false" />
                        <telerik:GridDateTimeColumn DataField="DateID" DataFormatString="ddMMyyyy" HeaderText="Ngày OT"
                            HeaderStyle-Width="8%" ItemStyle-HorizontalAlign="Center" DataType="System.DateTime">
                        </telerik:GridDateTimeColumn>
                        <telerik:GridBoundColumn DataField="TimeIn" HeaderText="Giờ vào(hh:mm)" ItemStyle-HorizontalAlign="Center"
                            HeaderStyle-Width="5%">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="TimeOut" HeaderText="Giờ ra(hh:mm)" ItemStyle-HorizontalAlign="Center"
                            HeaderStyle-Width="5%">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="NumOfBreak" HeaderText="Nghỉ giữa ca(phút)" ItemStyle-HorizontalAlign="Center"
                            HeaderStyle-Width="5%">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="TotalOTHours" HeaderText="Tổng giờ đăng ký OT"
                            ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="5%">
                        </telerik:GridBoundColumn>
                        <telerik:GridTemplateColumn HeaderText="Nghỉ bù" HeaderStyle-Width="4%" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:CheckBox runat="server" ID="chkNghiBu" />
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridBoundColumn DataField="OTHoursDay" HeaderText="OT ngày (thực tế)" HeaderStyle-Width="5%"
                            ItemStyle-HorizontalAlign="Center">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="OTHoursNight" HeaderText="OT đêm (thực tế)" HeaderStyle-Width="5%"
                            ItemStyle-HorizontalAlign="Center">
                        </telerik:GridBoundColumn>
                        <telerik:GridTemplateColumn HeaderText="OT ngày (duyệt)" HeaderStyle-Width="5%">
                            <ItemTemplate>
                                <div style="display: none">
                                    <telerik:RadTextBox MaxLength="4" ID="txtOTHoursDay" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "OTHoursDay")%>'
                                        Width="100%" CssClass="inputText">
                                    </telerik:RadTextBox>
                                </div>
                                <telerik:RadNumericTextBox MaxLength="4" ID="txtOTHoursDayApprove" runat="server"
                                    Text='<%#DataBinder.Eval(Container.DataItem, "OTHoursDayApprove")%>' Width="100%"
                                    CssClass="inputText" NumberFormat-DecimalSeparator=".">
                                    <ClientEvents OnValueChanged="txtOTHoursDayApprove_OnTextChanged" />
                                </telerik:RadNumericTextBox>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn HeaderText="OT đêm (duyệt)" HeaderStyle-Width="5%">
                            <ItemTemplate>
                                <div style="display: none">
                                    <telerik:RadTextBox MaxLength="4" ID="txtOTHoursNight" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "OTHoursNight")%>'
                                        Width="100%" CssClass="inputText">
                                    </telerik:RadTextBox>
                                </div>
                                <telerik:RadTextBox MaxLength="4" ID="txtOTHoursNightApprove" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "OTHoursNightApprove")%>'
                                    Width="100%" CssClass="inputText">
                                    <ClientEvents OnValueChanged="txtOTHoursNightApprove_OnTextChanged" />
                                </telerik:RadTextBox>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridBoundColumn DataField="Status" HeaderText="Trạng thái" HeaderStyle-Width="6%"
                            ItemStyle-HorizontalAlign="Center">
                        </telerik:GridBoundColumn>
                        <telerik:GridDateTimeColumn DataField="SubmitDate" DataFormatString="ddMMyyyy" HeaderText="Ngày gửi"
                            HeaderStyle-Width="8%" ItemStyle-HorizontalAlign="Center" DataType="System.DateTime">
                        </telerik:GridDateTimeColumn>
                        <telerik:GridDateTimeColumn DataField="ApproveDate" DataFormatString="ddMMyyyy" HeaderText="Ngày duyệt"
                            HeaderStyle-Width="8%" ItemStyle-HorizontalAlign="Center" DataType="System.DateTime">
                        </telerik:GridDateTimeColumn>
                        <telerik:GridBoundColumn DataField="ApproveUser" HeaderText="Người duyệt" HeaderStyle-Width="6%"
                            ItemStyle-HorizontalAlign="Center">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Note" HeaderText="Ghi chú" HeaderStyle-Width="7%">
                        </telerik:GridBoundColumn>
                    </Columns>
                </MasterTableView>
                <ClientSettings EnableRowHoverStyle="true">
                    <Selecting AllowRowSelect="True" />
                    <Scrolling AllowScroll="True" UseStaticHeaders="True" SaveScrollPosition="True" FrozenColumnsCount="1">
                    </Scrolling>
                </ClientSettings>
            </telerik:RadGrid>
            <core:CoreDataSource ID="VacationList" runat="server" SelectMethod="GetOTByOTID"
                TypeName="vHCS.TS.Business.TS_OverTimeCollection" EnableCaching="False" MaximumRowsParameterName="">
                <SelectParameters>
                    <asp:ControlParameter Name="p_EmpID" ControlID="txtEmpID" PropertyName="Text" DbType="Int32" />
                    <asp:ControlParameter Name="sOverTimeID" ControlID="txtOverTimeID" PropertyName="Text"
                        DbType="String" />
                </SelectParameters>
            </core:CoreDataSource>
        </div>
    </div>
    <div class="divClear">
    </div>
    <div class="formTitle">
        <div class="formToolbar">
            <asp:Button ID="btnSaveBot" runat="server" Text="Lưu" CssClass="btnSave" ValidationGroup="validateSave"
                OnClick="btnSave_Click" />
        </div>
    </div>
    <script type="text/javascript">
        // check chọn tự động khi nhập thông tin trên lưới
        function checkAuto(ObjSelected, ObjName) {
            var strIDCheck = ObjSelected.replace(ObjName, 'ClientSelectColumnSelectCheckBox');
            document.getElementById(strIDCheck).checked = true;
        }
        function txtOTHoursDayApprove_OnTextChanged(sender, arg) {
            checkAuto(sender.get_id(), 'txtOTHoursDayApprove');
            var idOTHoursDayApprove = sender.get_id()
            var idOTHoursNightApprove = sender.get_id().replace('txtOTHoursDayApprove', 'txtOTHoursNightApprove');
            var idOTHoursDay = sender.get_id().replace('txtOTHoursDayApprove', 'txtOTHoursDay');
            var idOTHoursNight = sender.get_id().replace('txtOTHoursDayApprove', 'txtOTHoursNight');
            checkValidateInfo(idOTHoursDayApprove, idOTHoursNightApprove, idOTHoursDay, idOTHoursNight, arg);
        }
        function txtOTHoursNightApprove_OnTextChanged(sender, arg) {
            checkAuto(sender.get_id(), 'txtOTHoursNightApprove');
            var idOTHoursNightApprove = sender.get_id()
            var idOTHoursDayApprove = sender.get_id().replace('txtOTHoursNightApprove', 'txtOTHoursDayApprove');
            var idOTHoursDay = sender.get_id().replace('txtOTHoursNightApprove', 'txtOTHoursDay');
            var idOTHoursNight = sender.get_id().replace('txtOTHoursNightApprove', 'txtOTHoursNight');
            checkValidateInfo(idOTHoursDayApprove, idOTHoursNightApprove, idOTHoursDay, idOTHoursNight, arg);
        }
        function checkValidateInfo(idOTHoursDayApprove, idOTHoursNightApprove, idOTHoursDay, idOTHoursNight, arg) {
            // Lay giá trị OT ngày duyệt .         
            var txtOTHoursDayApprove = $find(idOTHoursDayApprove);
            var valueOTHoursDayApprove = txtOTHoursDayApprove.get_value();

            // Lay giá trị OT đêm duyệt .         
            var txtOTHoursNightApprove = $find(idOTHoursNightApprove);
            var valueOTHoursNightApprove = txtOTHoursNightApprove.get_value();

            // Lay giá trị OT ngày thực .

            var txtOTHoursDay = $find(idOTHoursDay);
            var valueOTHoursDay = parseFloat(txtOTHoursDay.get_value().replace(',', '.'));

            // Lay giá trị OT đêm thực .         
            var txtOTHoursNight = $find(idOTHoursNight);
            var valueOTHoursNight = parseFloat(txtOTHoursNight.get_value().replace(',', '.'));

            valueOTHoursDayApprove = parseFloat(valueOTHoursDayApprove);
            //valueOTHoursDayApprove = parseFloat(valueOTHoursDayApprove.replace(',', '.'));
            valueOTHoursNightApprove = parseFloat(valueOTHoursNightApprove.replace(',', '.'));

            var tempDayApprove = valueOTHoursDayApprove % 0.5;
            var tempNightApprove = valueOTHoursNightApprove % 0.5;
            if ((valueOTHoursDayApprove == "" || valueOTHoursDayApprove == null) && (valueOTHoursDayApprove != 0)) { // bắt buộc nhập OT duyêt ngày
                document.getElementById("divValid").style.display = "none";
                document.getElementById("divOTDayApprove").style.display = "";
                document.getElementById("divCheckOtDay").style.display = "none";
                document.getElementById("divCheckOTNight").style.display = "none";
                document.getElementById("divOTNightApprove").style.display = "none";
                txtOTHoursDayApprove.focus();
                arg.IsValid = false;
                return false;
            }
            else if ((valueOTHoursDayApprove != null) && ((valueOTHoursDayApprove > valueOTHoursDay) || (valueOTHoursDayApprove < 0) /* || (tempDayApprove != 0)*/)) { // Kiem tra nhap OT duyet  ngày
                document.getElementById("divValid").style.display = "none";
                document.getElementById("divCheckOtDay").style.display = "";
                document.getElementById("divCheckOTNight").style.display = "none";
                document.getElementById("divOTNightApprove").style.display = "none";
                document.getElementById("divOTDayApprove").style.display = "none";
                arg.IsValid = false;
                return false;
            }
            else if ((valueOTHoursNightApprove == null || valueOTHoursNightApprove == "" || isNaN(valueOTHoursNightApprove)) && (valueOTHoursNightApprove != 0)) { // bắt buộc  nhập OT duyêt đêm
                document.getElementById("divValid").style.display = "none";
                document.getElementById("divOTNightApprove").style.display = "";
                document.getElementById("divOTDayApprove").style.display = "none";
                document.getElementById("divCheckOtDay").style.display = "none";
                document.getElementById("divCheckOTNight").style.display = "none";
                txtOTHoursNightApprove.focus();
                arg.IsValid = false;
                return false;
            }
            /* else if ((valueOTHoursNightApprove != null || valueOTHoursNightApprove != "") && (isNaN(valueOTHoursNightApprove))) { // Kiem tra nhap so
            document.getElementById("divValid").style.display = "";
            document.getElementById("divOTNightApprove").style.display = "none";
            document.getElementById("divOTDayApprove").style.display = "none";
            document.getElementById("divCheckOtDay").style.display = "none";
            document.getElementById("divCheckOTNight").style.display = "none";
            // txtOTHoursNightApprove.focus();
            arg.IsValid = false;
            return false;
            }
            else if ((valueOTHoursDayApprove != null || valueOTHoursDayApprove != "") && (isNaN(valueOTHoursDayApprove))) {
            document.getElementById("divValid").style.display = "";
            document.getElementById("divOTNightApprove").style.display = "none";
            document.getElementById("divOTDayApprove").style.display = "none";
            document.getElementById("divCheckOtDay").style.display = "none";
            document.getElementById("divCheckOTNight").style.display = "none";
            //   txtOTHoursNightApprove.focus();
            arg.IsValid = false;
            return false;
            }*/
            else if ((valueOTHoursNight != null) && ((valueOTHoursNightApprove > valueOTHoursNight) || (valueOTHoursNightApprove < 0) /* || (tempNightApprove != 0) */)) { // Kiem tra nhap OT duyet đêm
                document.getElementById("divValid").style.display = "none";
                document.getElementById("divCheckOtDay").style.display = "none";
                document.getElementById("divCheckOTNight").style.display = "";
                document.getElementById("divOTNightApprove").style.display = "none";
                document.getElementById("divOTDayApprove").style.display = "none";
                arg.IsValid = false;
                return false;
            }
            else {
                document.getElementById("divValid").style.display = "none";
                document.getElementById("divCheckOtDay").style.display = "none";
                document.getElementById("divCheckOTNight").style.display = "none";
                document.getElementById("divOTNightApprove").style.display = "none";
                document.getElementById("divOTDayApprove").style.display = "none";
                arg.IsValid = true;
                return true;
            }
        }

        //Kiểm tra nhập ít nhất 1 hàng trên lưới
        function dtgList_Validate(source, args) {
            //Kiểm tra đã chọn dòng cần Save?
            var grid = document.getElementById("<%= dtg.MasterTableView.ClientID %>");
            var startItemRow = 4;
            var lblValid = "";
            var flag = 0;
            if (grid.rows.length > 0) {
                //duyệt qua tất cả các dòng của grid
                for (i = 0; i < grid.rows.length; i++) {
                    var rowIndex = startItemRow + (i * 2);
                    var ctlIndex = null;
                    if (rowIndex < 10) ctlIndex = '0' + rowIndex.toString();
                    else ctlIndex = rowIndex.toString();

                    //Gán giá trị từ Header vào combo trên lưới (những dòng được check chọn)
                    // begin if 
                    if (document.getElementById('ctl00_MainContent_dtg_ctl00_ctl' + ctlIndex + '_ClientSelectColumnSelectCheckBox').checked == true) {
                        flag = 1;

                        var idOTHoursDayApprove = 'ctl00_MainContent_dtg_ctl00_ctl' + ctlIndex + '_txtOTHoursDayApprove';
                        var idOTHoursNightApprove = 'ctl00_MainContent_dtg_ctl00_ctl' + ctlIndex + '_txtOTHoursNightApprove';
                        var idOTHoursDay = 'ctl00_MainContent_dtg_ctl00_ctl' + ctlIndex + '_txtOTHoursDay';
                        var idOTHoursNight = 'ctl00_MainContent_dtg_ctl00_ctl' + ctlIndex + '_txtOTHoursNight';

                        checkValidateInfo(idOTHoursDayApprove, idOTHoursNightApprove, idOTHoursDay, idOTHoursNight, args);

                        if (args.IsValid == false) {
                            return false;
                        }

                    } // end if
                } // for
            } // if grid

            if (flag == 0) {
                args.IsValid = false;
                new Message().ShowObject(alertmsg_10081);
                return false;
            }
            else {
                args.IsValid = true;
                return true;
            }
        }
        function SaveCallback(args) {
            var win = new WindowManager().GetRadWindow();
            win.BrowserWindow.CallParentPageOT(args);
            win.close();
        }
        function ClosePage() {
            window.close();
        }
    </script>
</asp:Content>
